/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package crm.service.impl;
import crm.entity.Activity;
import crm.entity.Client;
import crm.entity.ContactPerson;
import crm.entity.Project;
import crm.entity.Role;
import crm.entity.User;
import crm.service.AppService;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import org.hibernate.Hibernate;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
/**
 *
 * @author abet
 */
@Service("appService")
@Transactional(readOnly=true)
public class AppServiceImpl implements AppService {

    @Autowired
    private SessionFactory sessionFactory;

    @Override
    @Transactional(readOnly=false)
    public void saveClient(Client client) {
        sessionFactory.getCurrentSession().saveOrUpdate(client);
    }

    @Override
    @Transactional(readOnly=false)
    public void deleteClient(Client client) {
        sessionFactory.getCurrentSession().delete(client);
    }

    @Override
    public List<Client> findAllClient() {
        return (List<Client>)sessionFactory.getCurrentSession()
                .createQuery("from Client").list();
    }

    @Override
    public Client findClientById(String id) {
        return (Client) sessionFactory.getCurrentSession()
               .createQuery("from Client c where c.cId=:cid")
               .setParameter("cid", id).uniqueResult();
    }

    @Override
    public Client findClientByName(String name) {
        return (Client) sessionFactory.getCurrentSession()
               .createQuery("from Client c where c.cName=: cn")
               .setParameter("cn", name).uniqueResult();
    }

    @Override
    public List<Client> findClientByCity(String city) {
        return  sessionFactory.getCurrentSession()
                .createQuery("from Client c where c.cCity=:cc")
                .setParameter("cc", city).list();
    }

    @Override
    public List<Client> findClientByCategory(String category) {
        return sessionFactory.getCurrentSession()
                .createQuery("from Client c where c.cCategory=:ccat")
                .setParameter("ccat", category).list();
    }

    @Override
    @Transactional(readOnly=false)
    public void saveCp(ContactPerson cp) {
        sessionFactory.getCurrentSession().saveOrUpdate(cp);
    }

    @Override
    @Transactional(readOnly=false)
    public void deleteClient(ContactPerson cp) {
        sessionFactory.getCurrentSession().delete(cp);
    }

    @Override
    public List<ContactPerson> findAllCp() {
         return (List<ContactPerson>)sessionFactory.getCurrentSession()
                .createQuery("from ContactPerson").list();
    }

    @Override
    public ContactPerson findCpById(String id) {
        return (ContactPerson) sessionFactory.getCurrentSession()
                .createQuery("from ContactPerson cp where cp.cpId=:cpid")
                .setParameter("cpid", id).uniqueResult();
    }

    @Override
    public ContactPerson findCpByName(String name) {
       return (ContactPerson) sessionFactory.getCurrentSession()
                .createQuery("from ContactPerson cp where cp.cpName=:cpn")
                .setParameter("cpn", name).uniqueResult();
    }

    @Override
    public List<ContactPerson> findContactPerson(String client) {
        return (List<ContactPerson>) sessionFactory.getCurrentSession()
                .createQuery("from ContactPerson where client.cId=:cid")
                .setParameter("cid", client).list();
    }

    @Override
    @Transactional(readOnly=false)
    public void saveAct(Activity act) {
        sessionFactory.getCurrentSession().saveOrUpdate(act);
    }

    @Override
    @Transactional(readOnly=false)
    public void deleteAct(Activity act) {
        sessionFactory.getCurrentSession().delete(act);
    }

    @Override
    public List<Activity> findAllActivity() {
        return (List<Activity>)sessionFactory.getCurrentSession()
                .createQuery("from Activity").list();
    }

    @Override
    public List<Activity> findActByDate(Date date) {
        return sessionFactory.getCurrentSession()
                .createQuery("from Activity a where a.actDate=:ad")
                .setDate("ad", date).list();
    }

    
    @Override
    @Transactional(readOnly=false)
    public void saveProject(Project project) {
       sessionFactory.getCurrentSession().saveOrUpdate(project);
    }

    @Override
    @Transactional(readOnly=false)
    public void deleteProject(Project project) {
       sessionFactory.getCurrentSession().delete(project);
    }

    @Override
    public List<Project> findAllProject() {
        return sessionFactory.getCurrentSession()
                .createQuery("from Project").list();
    }

    @Override
    public Project findProjectByContractNumber(String contnumb) {
        return (Project)sessionFactory.getCurrentSession()
               .createQuery("from Project p where p.prContractNumber=:pcn")
               .setParameter("pcn",contnumb).uniqueResult();
    }

    @Override
    public Project findProjectByName(String name) {
         return (Project)sessionFactory.getCurrentSession()
               .createQuery("from Project p where p.prName=:pn")
               .setParameter("pn",name).uniqueResult();
    }

    @Override
    public List<Project> findProjectByStatus(String status) {
        return sessionFactory.getCurrentSession()
                .createQuery("from Project p where p.prStatus=:ps")
                .setParameter("ps", status).list();
    }

    @Override
    public List<Activity> findActivity(String project) {
        return sessionFactory.getCurrentSession()
                .createQuery("from Activity where project.prId=:pid")
                .setParameter("pid", project).list();
    }

    @Override
    public List<Project> findProjectByPeriod(Date startdate, Date enddate) {
           List<Project> lpj=new ArrayList<Project>();
           lpj= sessionFactory.getCurrentSession()
                .createQuery("from Project p where p.prProgressRange = :ppr and p.prEndDate>=:sd and p.prEndDate<=:ed")
                .setString("ppr", "100%").setDate("sd", startdate).setDate("ed", enddate).list();
           return lpj;
    }
    
      @Override
    public List<Project> findProjectByPeriod2(Date startdate, Date enddate) {
           List<Project> lpj=new ArrayList<Project>();
           lpj= sessionFactory.getCurrentSession()
                .createQuery("from Project p where p.prEndDate between :sd and :ed ")
                .setDate("sd", startdate).setDate("ed", enddate).list();
           Hibernate.initialize(lpj);
           return lpj;
    }

    @Override
    public List<Project> findProjectByProgress(String progress) {
           return sessionFactory.getCurrentSession()
                .createQuery("from Project p where p.prProgressRange=:pr")
                .setParameter("pr", progress).list();
    }

    @Override
    public long hitungAchievementByPeriod(Date startdate, Date enddate) {
        List<Project> lpj=findProjectByPeriod(startdate, enddate);
        long total=0;
         for (Project totalP : lpj) {
             total += totalP.getPrAmount();
       }
        return total;
    }

    @Override
    @Transactional(readOnly=false)
    public void saveUser(User user) {
        sessionFactory.getCurrentSession().saveOrUpdate(user);
    }

    @Override
    public void deleteUser(User user) {
        sessionFactory.getCurrentSession().delete(user);
    }

    @Override
    public List<User> findAllUser() {
        return sessionFactory.getCurrentSession()
                .createQuery("from User").list();
    }

    @Override
    public void saveRole(Role role) {
        sessionFactory.getCurrentSession().saveOrUpdate(role);
    }

    @Override
    public void deleteRole(Role role) {
       sessionFactory.getCurrentSession().delete(role);
    }

    @Override
    public List<Role> findAllRole() {
         return sessionFactory.getCurrentSession()
                .createQuery("from Role").list();
    }

    @Override
    public long hitungProyeksiAchievementByPeriod(Date startdate, Date enddate) {
         List<Project> lpj=findProjectByPeriod2(startdate, enddate);
        long total=0;
         for (Project totalP : lpj) {
             total += totalP.getPrAmount();
       }
        return total;

    
}
}